package com.flycms.module.document.service;

import com.flycms.core.entity.PageBean;
import com.flycms.core.entity.PageVo;
import com.flycms.module.activity.model.Activity;
import com.flycms.module.document.dao.DocumentDao;
import com.flycms.module.document.dao.DocumentDetailDao;
import com.flycms.module.document.model.Document;
import com.flycms.module.document.model.DocumentDetail;
import com.flycms.module.document.param.DocQueryParam;
import com.flycms.module.document.param.DocumentParam;
import com.flycms.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class DocumentService {

    @Autowired
    private DocumentDao documentDao;
    @Autowired
    private DocumentDetailDao documentDetailDao;

    /**
     * 根据类型查询文档
     *
     * @param type 类型
     * @return 文档集合
     */
    public List<Document> getDocList(Integer type) {
        return documentDao.selectByType(type);
    }

    /**
     * 根据名称模糊匹配
     *
     * @param name 文档名称
     * @return 匹配结果
     */
    public List<Document> getDocByName(String name) {
        return documentDao.selectByName("%" + name + "%");
    }

    /**
     * 获取文档详情
     *
     * @param id 文档ID
     * @return 返回详情
     */
    public DocumentDetail getDocDetailById(Long id) {
        return documentDetailDao.getDocById(id);
    }

    /**
     * 获取文档详情
     *
     * @param id 文档ID
     * @return 返回详情
     */
    public Document getDocById(Long id) {
        return documentDao.getDocById(id);
    }

    /**
     * 保存文档信息
     *
     * @param param 文档数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(DocumentParam param) {
        Document document = ConvertUtils.sourceToTarget(param, Document.class);
        documentDao.insertSelective(document);
        DocumentDetail documentDetail = ConvertUtils.sourceToTarget(param, DocumentDetail.class);
        documentDetail.setDocId(document.getId());
        documentDetailDao.insertSelective(documentDetail);
    }

    public PageVo<Document> getPage(DocQueryParam param) {
        //统计数据
        int count = documentDao.count(param);
        if (count <= 0) {
            return new PageVo<>(param.getPageIndex());
        }
        //查询数据
        List<Document> documents =
                documentDao.selectPage(param,(param.getPageIndex() - 1) * param.getPageSize(), param.getPageSize());
        return new PageVo<>(param.getPageIndex(), count, documents);
    }

    @Transactional(rollbackFor = Exception.class)
    public void del(Long id) {
        documentDao.del(id);
        documentDetailDao.delByDocId(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(Document document) {
        documentDao.update(document);
        DocumentDetail detail = document.getDetail();
        if (Objects.nonNull(detail)) {
            if (Objects.isNull(detail.getId())) {
                detail.setDocId(document.getId());
                documentDetailDao.insertSelective(detail);
            } else {
                documentDetailDao.update(detail);
            }
        }
    }
}
